<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/7/22
 * Time: 11:29
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class  Notice extends Base {
    /**
     * 列表展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $map = array();
            extract(input());
            if(isset($keyword)&&$keyword!="")
            {
                $map['n_title'] = ['like',"%" . $keyword . "%"];
            }
            if(isset($status)&&$status!="")
            {
                $map['n_status'] = $status;
            }
            if(isset($start) && $start != '' && isset($end) && $end != ''){
                $map['n_time'] = ['between', [$start, $end]];
            }
            if(isset($start) && $start != '' && isset($end) && $end == ''){
                $map['n_time'] = ['> time', $start];
            }
            if(isset($start) && $start == '' && isset($end) && $end != ''){
                $map['n_time'] = ['< time', $end];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('notice')
                ->where($map)->count();// 获取总条数
            $lists = Db::name('notice')
                ->where($map)
                ->order('n_status asc')
                ->page($NowPage,$limits)
                ->select();
            foreach ($lists as $key=>&$vo) {
                if ($vo['n_type'] == 1) {//全部人员
                    $num = Db::table('up_u_user')->count();
                    $vo['num'] = $num;
                } else {
                    $n_user_id = trim($vo['n_user_id'], ',');
                    $num = Db::table('up_u_user')->where('user_id', 'IN', $n_user_id)->count();
                    $vo['num'] = $num;
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }


    public function add(){
        if(Request::instance()->isPost()){
            $user_id = input('param.user_id');
            $content = input('param.content');
            $m_title = input('param.m_title');
            $n_type = input('param.n_type');
            $publish = input('param.n_publish_time');//1是  2否
            if($publish==1){//设置发布时间
                $data['n_status'] = 1;
                $data['n_publish_time'] = input('param.start');
                if($data['n_publish_time']==''||$data['n_publish_time']==null){
                    return json(['code' => 100, 'data' => '', 'msg' => '发布时间不能为空']);
                }else{
                    if($data['n_publish_time']<date('Y-m-d H:i:s',time())){
                        return json(['code' => 100, 'data' => '', 'msg' => '发布时间不能小于当前时间']);
                    }
                }
            }else{
                $data['n_status'] = 2;
                $data['n_publish_time'] = date('Y-m-d H:i:s',time());
            }
            $data['n_title'] =$m_title;
            $data['n_content'] =$content;
            $data['n_type'] =$n_type;
            if($n_type==2){
                $data['n_user_id'] =','.$user_id.',';
            }
            $data['n_read'] =',0,';
            $data['n_delete'] =',0,';
            $data['n_user_id'] =',0,';
            $data['n_time'] =date('Y-m-d H:i:s',time());
            $res = Db::table('up_notice')->insertGetId($data);

            if($res){
                if($publish == 2){//立刻发送，则推送
                    $str = array();
                    if($n_type ==2){//推送个人
                        $m_ids = explode(',',$user_id);//所有推送的人
                        $list = [];
                        foreach($m_ids as $k => $val){
                            $v = (string)$val;
                            array_push($list, $v);
                        }
                        try{
                            $this->Jpush1(1,$list,$m_title,$str,1);
                        }catch(\Exception $e){

                        }
                    }elseif ($n_type ==1){//推送全部
                        $list = [];
                        try{
                            $this->Jpush1(2,$list,$m_title,$str,1);
                        }catch(\Exception $e){

                        }
                    }
                }
                return json(['code' => 200, 'msg' => '保存成功']);
            }else{
                return json(['code' => 100, 'msg' => '保存失败']);
            }
        }
        $info = Db::table('up_u_user')->field('user_id,user_username')->select();
        $this->assign('info',$info);
        return $this->fetch();
    }

    public function update(){

        if(Request::instance()->isPost()){
            $id= input('param.id');
            $user_id = input('param.user_id');
            $content = input('param.content');
            $m_title = input('param.m_title');
            $n_type = input('param.n_type');
            $publish = input('param.n_publish_time');//1是  2否
            if($publish==1){//设置发布时间
                $data['n_status'] = 1;
                $data['n_publish_time'] = input('param.start');
                if($data['n_publish_time']==''||$data['n_publish_time']==null){
                    return json(['code' => 100, 'data' => '', 'msg' => '发布时间不能为空']);
                }else{
                    if($data['n_publish_time']<date('Y-m-d H:i:s',time())){
                        return json(['code' => 100, 'data' => '', 'msg' => '发布时间不能小于当前时间']);
                    }
                }
            }else{
                $data['n_status'] = 2;
                $data['n_publish_time'] = date('Y-m-d H:i:s',time());
            }
            $data['n_title'] =$m_title;
            $data['n_content'] =$content;
            $data['n_type'] =$n_type;
            if($n_type==2){
                $data['n_user_id'] =','.$user_id.',';
            }elseif ($n_type==1){
                $data['n_user_id'] ='';
            }
            $data['n_read'] =',0,';
            $data['n_delete'] =',0,';
            $data['n_time'] =date('Y-m-d H:i:s',time());
            $res = Db::table('up_notice')->where('n_id',$id)->update($data);

            if($res){
                if($publish == 2){//立刻发送，则推送
                    $str = array();
                    if($n_type ==2){//推送个人
                        $m_ids = explode(',',$user_id);//所有推送的人
                        $list = [];
                        foreach($m_ids as $k => $val){
                            $v = (string)$val;
                            array_push($list, $v);
                        }
                        try{
                            $this->Jpush1(1,$list,$m_title,$str,1);
                        }catch(\Exception $e){

                        }
                    }elseif ($n_type ==1){//推送全部
                        $list = [];
                        try{
                            $this->Jpush1(2,$list,$m_title,$str,1);
                        }catch(\Exception $e){

                        }
                    }
                }
                return json(['code' => 200, 'msg' => '保存成功']);
            }else{
                return json(['code' => 100, 'msg' => '保存失败']);
            }
        }
        $id = input('param.n_id');
        $info = Db::table('up_notice')->where('n_id',$id)->find();
        $userList  = explode(',',trim($info['n_user_id'],','));
        $info1 = Db::table('up_u_user')->field('user_id,user_username')->select();
        $this->assign('id',$id);
        $this->assign('info',$info);
        $this->assign('info1',$info1);
        $this->assign('userList',$userList);
        return $this->fetch();
    }
    /*****
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $id = input('param.id');
        $res=  Db::table('up_notice')->where('n_id',$id)->delete();
        if($res){
            return json(['code' => 200, 'data' => '', 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'data' => '', 'msg' => '删除失败']);
        }
    }
    /*****
     * @return \think\response\Json
     * 详情
     */
    public function detail(){
        $n_id = input('param.n_id');
        $n_content = Db::table('up_notice')->where('n_id',$n_id)->find();
        $this->assign('res',$n_content);
        return  $this->fetch();
    }
}